package com.njtoyo.taxi.admin.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class DESede {
    /**
     * 加密算法
     */
    private static final String KEY_ALGORITHM = "DESede";
    /**
     * 加密/解密算法/工作模式/填充模式
     * Java6 支持PKCS5Padding填充方式
     * Bouncy Castle支持PCKS7Padding填充方式
     * DESede/CBC/PKCS5Padding需要偏移量
     * DESede/ECB/PKCS5Padding
     */
    private static final String CIPHER_ALGORITHM = "DESede/CBC/PKCS5Padding";

    /**
     * 3DES 加密
     *
     * @param key   秘钥（24位）
     * @param iv    偏移量
     * @param data  需要加密的字符串
     * @return 返回加密的字符串
     */
    public static String encrypt(String key, String iv, String data) {
        try {
            DESedeKeySpec spec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            Key deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            //cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byte[] bOut = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            return Base64.encodeBase64String(bOut);
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("3DES 解密错误：{}", e);
            throw new RuntimeException("3DES 解密错误");
        }
    }

    /**
     * 3DES 解密
     *
     * @param key   秘钥（24位）
     * @param iv    偏移量
     * @param data  需要解密的密文
     * @return 返回加密的字符串
     */
    public static String decrypt(String key, String iv, String data) {
        try {
            DESedeKeySpec spec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            Key deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            //cipher.init(Cipher.DECRYPT_MODE, deskey);
            byte[] bOut = cipher.doFinal(Base64.decodeBase64(data.getBytes(StandardCharsets.UTF_8)));
            return new String(bOut, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("3DES 解密错误：{}", e);
            throw new RuntimeException("3DES 解密错误");
        }
    }

    public static void main(String[] args) {
        String data = "{\"phoneNum\":\"13811111111\",\"cityCode\":210001,\"carNum\":\"苏AB8888\"}";

        String des3EncodeCBC = encrypt("wj9LtuJksPHS4G0yppNuSy62", "20240101", data);
        System.out.println("加密后的字符串内容为：" + des3EncodeCBC);

        String des3DecodeCBC = decrypt("wj9LtuJksPHS4G0yppNuSy62", "20240101", "Ea+iYB0sEmzuGupFoE6AnBPZZ65zLMaH6ykllUxbtDXMJPUhJ99InqhGl1XrCNMPFom/nPHVqc+P9kgtkGgYZPSfLJrGYDZk");
        System.out.println("解密后的字符串内容为：" + des3DecodeCBC);
       // System.out.println(IdUtils.fastSimpleUUID());
    }

}
